﻿using System;
using System.Data;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Windows.Forms;
using VMExplorer.Controls;
using VMExplorer.Properties;

namespace VMExplorer.Forms
{
  partial class MainForm : Form
  {
    #region Private fields

    //private VMEDocument _doc;
    private string _baseTitle;

    #endregion
    #region Public properties

    public static VMEDocument Doc { get; private set; }

    #endregion
    #region Construction

    public MainForm ()
    {
      InitializeComponent ();

      AssemblyName asmName = Assembly.GetExecutingAssembly ().GetName ();
      _baseTitle = String.Format ("{0} {1}.{2}", asmName.Name, asmName.Version.Major, asmName.Version.Minor);
      this.Text = _baseTitle;

      splitContainer1.Panel1.Controls.Add (new DockPanel (_toolMemObjects, "Memory objects"));
      splitContainer3.Panel2.Controls.Add (new DockPanel (_toolPageDump, "Page dump"));
    }

    #endregion
    #region Load process

    private void LoadProcess (Process p)
    {
      Doc = new VMEDocument ();

      _toolMemoryMap.Document = Doc;
      _toolMemObjects.Document = Doc;
      _toolPageDump.Document = Doc;

      Doc.Load (p);
      Doc.AutoUpdate = _toolMemoryMap.AutoUpdate;

      this.Text = string.Format ("{1} ({2}) - {0}", _baseTitle, p.MainModule.ModuleName, p.Id);
    }

    #endregion
    #region WinForm events

    private void MainForm_Load (object sender, EventArgs e)
    {
      this.Location = Settings.Default.StartLocation;
      this.Size = Settings.Default.StartSize;
      LoadProcess (Process.GetCurrentProcess ());
    }

    private void MainForm_FormClosing (object sender, FormClosingEventArgs e)
    {
      Settings.Default.StartSize = this.Size;
      Settings.Default.StartLocation = this.Location;
      Settings.Default.Save ();
    }

    #endregion
    #region Menu items

    private void openProcess_Click (object sender, EventArgs e)
    {
      ProcessList pl = new ProcessList ();
      if (pl.ShowDialog () != DialogResult.Cancel)
      {
        LoadProcess (Process.GetProcessById (pl.Id));
      }
    }

    private void runProcess_Click (object sender, EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog ();
      dlg.Multiselect = false;
      dlg.Filter = "Executable file (*.exe)|*.exe|All files (*.*)|*.*";

      DialogResult result = dlg.ShowDialog ();
      if (result == DialogResult.OK)
      {
        Process p = new Process ();
        p.StartInfo.FileName = dlg.FileName;
        p.Start ();
        LoadProcess (p);
      }
    }

    private void goToOffset_Click (object sender, EventArgs e)
    {
      GoToDialog dlg = new GoToDialog ();
      while (dlg.ShowDialog () == DialogResult.OK)
      {
        try
        {
          int userAddress = Int32.Parse (dlg.UserOffset.Text, NumberStyles.HexNumber);
          Doc.SelectedAddress = new MemoryAddress (new IntPtr (userAddress));
          break;
        }
        catch
        {
        }
      }
    }

    private void generateMemoryReport_Click (object sender, EventArgs e)
    {
      new ReportForm (Doc).Show ();
    }

		private void about_Click (object sender, EventArgs e)
		{
			new About ().ShowDialog ();
		}

    //private void openDumpToolStripMenuItem_Click (object sender, EventArgs e)
    //{
    //  OpenFileDialog dlg = new OpenFileDialog ();
    //  dlg.Multiselect = false;
    //  dlg.Filter = "Dump file (*.dmp)|*.dmp|All files (*.*)|*.*";

    //  DialogResult result = dlg.ShowDialog ();
    //  if (result == DialogResult.OK)
    //  {
    //    Debugger dbg = new Debugger ();
    //    dbg.OpenDumpFile (dlg.FileName);
    //    dbg.DumpStack ();

    //    dbg.Dispose ();
    //  }
    //}

    #endregion
  }
}
